#include "storeregister.h"
#include "ui_storeregister.h"

StoreRegister::StoreRegister(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::StoreRegister)
{

    ui->setupUi(this);
    QDate date;
    ui->Edit_indate->setText(date.toString());
}

StoreRegister::~StoreRegister()
{
    delete ui;
}

void StoreRegister::on_Ensure_clicked()
{
    QString storeInputAccount = ui->Edit_account->text();
    QString storeInputPassword = ui->Edit_password->text();
    //判断用户名和密码是否为空
    if(storeInputAccount==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Account can't be NULL!");
    }
    else if (storeInputPassword==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Password can't be NULL!");
    }
    else
    {
        QSqlQuery sql;

        sql.prepare("SELECT * FROM tb_store WHERE saccount = :storeInputAccount");
        sql.bindValue(":storeInputAccount", storeInputAccount);
        sql.exec();

        if(sql.first()){
        //如果用户名重复，则提示用户名重复
            QMessageBox::warning(this,"ERROR","用户名重复");//如果用户名重复，则提示用户名重复
        }
        else{
        //如果用户名不重复，添加数据进入数据库
            sql.prepare("INSERT INTO tb_store(saccount,spassword,sname,sindate,stele,saddress) values(:storeInputAccount,:storeInputPassword,:storeName,:storeIndate,:storeTele,:storeAddress)");
            sql.bindValue(":storeInputAccount", storeInputAccount);
            sql.bindValue(":storeInputPassword", storeInputPassword);
            sql.bindValue(":storeName", ui->Edit_sname->text());
            QDate date;
            sql.bindValue(":storeIndate", date.currentDate());
            sql.bindValue(":storeTele", ui->Edit_stele->text());
            sql.bindValue(":storeAddress", ui->Edit_address->text());
            sql.exec();
            QMessageBox::information(this,"提示","注册成功！！",QMessageBox::Yes);
            this->close();
        }
    }
}

